package com.sxx.mall.ware.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxx.commons.dto.SkuReductionDTO;
import com.sxx.commons.utils.PageUtils;
import com.sxx.commons.utils.Query;
import com.sxx.mall.ware.dao.PurchaseDetailDao;
import com.sxx.mall.ware.entity.PurchaseDetailEntity;
import com.sxx.mall.ware.service.PurchaseDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {


    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> entities = this.
                list(new QueryWrapper<PurchaseDetailEntity>()
                        .eq("purchase_id", id));
        return entities;
    }

    //查询 key=&status=&wareId=
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key)
                    .or().eq("purchase_id",key)
                    .or().eq("sku_id",key)
                    .or().eq("sku_num",key)
                    .or().like("sku_price",key);
        }
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void savePurchaseDetail(PurchaseDetailEntity purchaseDetail) {
        PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
        BeanUtils.copyProperties(purchaseDetail,purchaseDetailEntity);
        //TODO 保存远程调用skuInfoService,拿到sku_price保存
        Integer skuNum = purchaseDetail.getSkuNum();
        Long skuId = purchaseDetail.getSkuId();

        this.save(purchaseDetailEntity);
    }

}